//
//  HappyChildrenTabBarController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HappyChildrenTabBarController.h"

@interface HappyChildrenTabBarController ()

@property (nonatomic, strong) NSMutableArray *tabBarmutableArray;

@property (nonatomic, strong) HCTabBarView *currentTabBarView;

@end

@implementation HappyChildrenTabBarController

@synthesize delegate;

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = YBKRGBCOLOR(253, 253, 253, 1);
        self.tabBarmutableArray = [NSMutableArray arrayWithCapacity:0];
    }
    return self;
}

#pragma mark setter
-(void)setTabsArray:(NSArray *)tabsArray {
    for (UIView *vv in [self subviews]) {
        [vv removeFromSuperview];
    }
    
    tabsArray_ = tabsArray;
    NSInteger count = tabsArray_.count;
    CGFloat width  = (CGFloat)((self.frame.size.width) / count);
    CGRect makeRect;
    
    for (NSInteger i = 0; i<count; i++) {
        
        NSDictionary *dictionary = [tabsArray_ objectAtIndex:i];
        NSString *tagID = [dictionary objectForKey:@"MenuID"];
        NSString *nomalImage = [dictionary objectForKey:@"nomalImage"];
        NSString *selectImage = [dictionary objectForKey:@"selectImage"];
        makeRect = CGRectMake(0+i*width+i*1, 0, width, self.frame.size.height);
        
        HCTabBarView *tabBarView = [[HCTabBarView alloc] initWithFrame:makeRect];
        tabBarView.tag = [tagID integerValue];
        tabBarView.target = self;
        tabBarView.selectImageName = selectImage;
        tabBarView.nomalImageName = nomalImage;
        tabBarView.targetSel = NSSelectorFromString(@"tabBarSelectAction:");
        [self.tabBarmutableArray addObject:tabBarView];
    }
    
    if (self.tabBarmutableArray.count) {
        HCTabBarView *tabBarView;
        for (NSInteger i=self.tabBarmutableArray.count-1; i<self.tabBarmutableArray.count; i--) {
            tabBarView = (HCTabBarView *)[self.tabBarmutableArray objectAtIndex:i];
            [self addSubview:tabBarView];
        }
        tabBarView.selected = YES;      //默认第一个高亮
        self.currentTabBarView = tabBarView;
    }
}

#pragma mark getter
-(NSArray *)tabsArray {
    return tabsArray_;
}

-(void)tabBarSelectAction:(id)object {
    if ([object isKindOfClass:[HCTabBarView class]]) {
        HCTabBarView *btn = (HCTabBarView *)object;
        if (btn.tag != self.currentTabBarView.tag) {  //这里只有在tag不同的时候才调用
            self.currentTabBarView.selected = NO;
            self.currentTabBarView = btn;
            self.currentTabBarView.selected = YES;
            if ([self.delegate respondsToSelector:@selector(tabBarSelectedView:indePath:)]&&self.delegate) {
                [self.delegate tabBarSelectedView:self indePath:btn.tag];
            }
        }
    }
}

-(void)selectedIndexPath:(NSInteger)index {
    
    HCTabBarView *selectedTab = (HCTabBarView *)[self viewWithTag:index];
    if (selectedTab.target) {
        SuppressPerformSelectorLeakWarning([selectedTab.target performSelector:selectedTab.targetSel withObject:selectedTab]);
    }
}

-(void)dealloc {
    self.tabBarmutableArray = nil;
    self.tabsArray = nil;
    self.delegate = nil;
}

@end

#pragma mark -- tabbarView 
@interface HCTabBarView ()

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation HCTabBarView

@synthesize target;
@synthesize targetSel;

-(void)dealloc {
    
    self.imageView = nil;
    self.tabBarBtn = nil;
    self.target = nil;
    self.targetSel = nil;
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.tabBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.tabBarBtn.frame = self.bounds;
        self.tabBarBtn.backgroundColor = [UIColor clearColor];
        [self.tabBarBtn addTarget:self action:@selector(selectedAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.tabBarBtn];
        
        //图片的宽度为49*49
        self.showImage = [[UIImageView alloc]initWithFrame:CGRectMake((YHGetSizeWidth(self)-49)/2.0, (YHGetSizeHeight(self)-40)/2.0, 40, 40)];
        _showImage.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.showImage];
    }
    return self;
}

#pragma mark 设置默认图片
-(void)setNomalImageName:(NSString *)nomalImageName {
    _nomalImageName = nomalImageName;
    
    if ([nomalImageName hasPrefix:@"http://"]) {//如果是传入的网络图片则下载来加载
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:nomalImageName] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            if (finished) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.showImage.image = image;
                });
            }
        }];
    }else{
        self.showImage.image = [UIImage imageNamed:nomalImageName];
    }
}

#pragma mark 设置高亮图片
-(void)setSelectImageName:(NSString *)selectImageName {
    _selectImageName=selectImageName;
    if ([selectImageName hasPrefix:@"http://"]) {
        [[SDWebImageDownloader  sharedDownloader]downloadImageWithURL:[NSURL URLWithString:selectImageName] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.showImage.highlightedImage=image;
            });
            
        }];
    }else{
        self.showImage.highlightedImage=[UIImage imageNamed:selectImageName];
    }
}

-(NSString *)tabBarTitle {
    return tabBarTitle;
}

-(void)setSelected:(BOOL)selected {
    _selected = selected;
    if (_selected) {
        //选中
        self.showImage.highlighted = YES;
    }else{
        self.showImage.highlighted = NO;
    }
}

-(void)selectedAction:(UIButton*)sender{
    
    if(self.target){
        SuppressPerformSelectorLeakWarning([self.target performSelector:self.targetSel withObject:self]);
    }
}

@end
